/* main.c */
#include "app_inc.h"

#define DEMO_LPIT_SPEED_SENSOR_CHANNEL 0U

volatile uint32_t motor_speed;
volatile uint32_t left_feedback, right_feedback;
volatile bool bEnIncrease;
volatile Motor_Direction_T gMotorDirection;

void LPIT_Configuration(void);

int main(void)
{
    BSP_InitSystem();
    BSP_InitStdioLPUART(115200U);
    BSP_InitMotorTurn();
    BSP_InitMotorQuadDecoder();

    LPIT_Configuration();
    
    printf("\r\n\r\nMotorControl Demo.\r\n");
    printf("Compiled on %s, %s\r\n\r\n", __DATE__, __TIME__);

    Motor_InitDevice();
    Motor_EnableTurn(true);

    motor_speed = 0;
    bEnIncrease = true;
    gMotorDirection = eMotor_DirectionForward;
    Motor_SetTurnDirection(MOTOR_ID_A, gMotorDirection);
    Motor_SetTurnDirection(MOTOR_ID_B, gMotorDirection);
    
    printf("Press any key to change the speed.\r\n");
    LPIT_HAL_StartChannel(LPIT0, DEMO_LPIT_SPEED_SENSOR_CHANNEL);
    while (1)
    {
        /* Change speed. */
        getchar();
        Motor_SetTrunSpeed(MOTOR_ID_A, motor_speed);
        Motor_SetTrunSpeed(MOTOR_ID_B, motor_speed);
        
        /* Show current speed. */
        getchar();
        printf("left_feedback: %5d\tright_feedback: %5d\r\n", left_feedback, right_feedback);

        /* Update speed value. */
        if (bEnIncrease)
        {
            motor_speed += 250U;
        }
        else
        {
            motor_speed -= 250U;
        }
        if (motor_speed >= MOTOR_PWM_COUNTER_MAX)
        {
            bEnIncrease = false;
        }
        if (motor_speed == 0U)
        {
            bEnIncrease = true;
            if (gMotorDirection == eMotor_DirectionForward)
            {
                gMotorDirection = eMotor_DirectionBackward;
            } else if (gMotorDirection == eMotor_DirectionBackward)
            {
                gMotorDirection = eMotor_DirectionForward;
            }
            Motor_SetTurnDirection(MOTOR_ID_A, gMotorDirection);
            Motor_SetTurnDirection(MOTOR_ID_B, gMotorDirection);
        }
    }
}

void LPIT_Configuration(void)
{
    LPIT_ChannelConfig_T lpitChannelConfigStruct;

    PCC->CLKCFG[PCC_LPIT0_INDEX] = PCC_CLKCFG_CGC_MASK | PCC_CLKCFG_PCS(6U); /* PLL. */
    /* LPIT. */
    LPIT_HAL_Enable(LPIT0, true);
    LPIT_HAL_Reset(LPIT0);
    LPIT_HAL_GetChannelConfigForNormalTimer(&lpitChannelConfigStruct);

    /* 配置触发调整小车行进的周期定时器 */
    LPIT_HAL_ConfigChannel(LPIT0, DEMO_LPIT_SPEED_SENSOR_CHANNEL, &lpitChannelConfigStruct);
    LPTI_HAL_SetChannelTimeoutValue(LPIT0, DEMO_LPIT_SPEED_SENSOR_CHANNEL, BSP_CLK_SPLLDIV2_HZ/50); /* BSP_CLK_SPLLDIV2_HZ = 75MHz, 20ms, 50Hz */
    LPIT_HAL_EnableChannelInterrupt(LPIT0, DEMO_LPIT_SPEED_SENSOR_CHANNEL, true);

    NVIC_EnableIRQ(LPIT0_IRQn);
}

void LPIT0_IRQHandler(void)
{
    if (LPIT_HAL_GetChannelTimeoutFlag(LPIT0, DEMO_LPIT_SPEED_SENSOR_CHANNEL))
    {
        LPTI_HAL_ClearChannelTimeoutFlag(LPIT0, DEMO_LPIT_SPEED_SENSOR_CHANNEL);
        
        left_feedback = Motor_GetQuadDecoderValue(MOTOR_ID_A);
        Motor_ClearQuadDecoderValue(MOTOR_ID_A);
        right_feedback = Motor_GetQuadDecoderValue(MOTOR_ID_B);
        Motor_ClearQuadDecoderValue(MOTOR_ID_B);
    }
}


/* EOF. */

